Add specs for permanent urls in the DropboxFileUrlAgent

Dominik Sander 8 年之前
父节点
当前提交
d4008ba4d2
共有 2 个文件被更改,包括 63 次插入32 次删除
  1. 2 2
      app/models/agents/dropbox_file_url_agent.rb
  2. 61 30
      spec/models/agents/dropbox_file_url_agent_spec.rb

+ 2 - 2
app/models/agents/dropbox_file_url_agent.rb

@@ -52,8 +52,8 @@ module Agents
52 52
     end
53 53
 
54 54
     def receive(events)
55
-      events.map { |e| e.payload['paths'].split(',').map(&:strip) }
56
-        .flatten.each do |path|
55
+      events.flat_map { |e| e.payload['paths'].split(',').map(&:strip) }
56
+        .each do |path|
57 57
           create_event payload: (options['link_type'] == 'permanent' ? permanent_url_for(path) : temporary_url_for(path))
58 58
         end
59 59
     end

+ 61 - 30
spec/models/agents/dropbox_file_url_agent_spec.rb

@@ -24,51 +24,82 @@ describe Agents::DropboxFileUrlAgent do
24 24
   end
25 25
 
26 26
   describe "#receive" do
27
-
28
-    let(:first_dropbox_url_payload)  { { 'url' => 'http://dropbox.com/first/path/url' } }
29
-    let(:second_dropbox_url_payload) { { 'url' => 'http://dropbox.com/second/path/url' } }
30
-    let(:third_dropbox_url_payload)  { { 'url' => 'http://dropbox.com/third/path/url' } }
31
-
32
-    def create_event(payload)
27
+    def event(payload)
33 28
       event = Event.new(payload: payload)
34 29
       event.agent = agents(:bob_manual_event_agent)
35
-      event.save!
36 30
       event
37 31
     end
38 32
 
39
-    before(:each) do
40
-      stub.proxy(Dropbox::API::Client).new do |api|
41
-        stub(api).find('/first/path')  { stub(Dropbox::API::File.new).direct_url { first_dropbox_url_payload } }
42
-        stub(api).find('/second/path') { stub(Dropbox::API::File.new).direct_url { second_dropbox_url_payload } }
43
-        stub(api).find('/third/path')  { stub(Dropbox::API::File.new).direct_url { third_dropbox_url_payload } }
33
+    context 'with temporaty urls' do
34
+      let(:first_dropbox_url_payload)  { { 'url' => 'http://dropbox.com/first/path/url' } }
35
+      let(:second_dropbox_url_payload) { { 'url' => 'http://dropbox.com/second/path/url' } }
36
+      let(:third_dropbox_url_payload)  { { 'url' => 'http://dropbox.com/third/path/url' } }
37
+
38
+      before(:each) do
39
+        stub.proxy(Dropbox::API::Client).new do |api|
40
+          stub(api).find('/first/path')  { stub(Dropbox::API::File.new).direct_url { first_dropbox_url_payload } }
41
+          stub(api).find('/second/path') { stub(Dropbox::API::File.new).direct_url { second_dropbox_url_payload } }
42
+          stub(api).find('/third/path')  { stub(Dropbox::API::File.new).direct_url { third_dropbox_url_payload } }
43
+        end
44 44
       end
45
-    end
46
-
47
-    context 'with a single path' do
48 45
 
49
-      before(:each) { @event = create_event(paths: '/first/path') }
46
+      context 'with a single path' do
47
+        before(:each) { @event = event(paths: '/first/path') }
50 48
 
51
-      it 'creates one event with the temporary dropbox link' do
52
-        expect { @agent.receive([@event]) }.to change(Event, :count).by(1)
53
-        expect(Event.last.payload).to eq(first_dropbox_url_payload)
49
+        it 'creates one event with the temporary dropbox link' do
50
+          expect { @agent.receive([@event]) }.to change(Event, :count).by(1)
51
+          expect(Event.last.payload).to eq(first_dropbox_url_payload)
52
+        end
54 53
       end
55 54
 
55
+      context 'with multiple comma-separated paths' do
56
+        before(:each) { @event = event(paths: '/first/path, /second/path, /third/path') }
57
+
58
+        it 'creates one event with the temporary dropbox link for each path' do
59
+          expect { @agent.receive([@event]) }.to change(Event, :count).by(3)
60
+          last_events = Event.last(3)
61
+          expect(last_events[0].payload).to eq(first_dropbox_url_payload)
62
+          expect(last_events[1].payload).to eq(second_dropbox_url_payload)
63
+          expect(last_events[2].payload).to eq(third_dropbox_url_payload)
64
+        end
65
+      end
56 66
     end
57 67
 
58
-    context 'with multiple comma-separated paths' do
68
+    context 'with permanent urls' do
69
+      def response_for(url)
70
+        Dropbox::API::Object.new(
71
+          url:  "https://www.dropbox.com/s/#{url}?dl=0",
72
+          expires: "Tue, 01 Jan 2030 00:00:00 +0000",
73
+          visibility: "PUBLIC"
74
+        )
75
+      end
59 76
 
60
-      before(:each) { @event = create_event(paths: '/first/path, /second/path, /third/path') }
77
+      let(:first_dropbox_url_payload)  { response_for('/first/path') }
78
+      let(:second_dropbox_url_payload) { response_for('/second/path') }
79
+      let(:third_dropbox_url_payload)  { response_for('/third/path') }
80
+
81
+      before(:each) do
82
+        stub.proxy(Dropbox::API::Client).new do |api|
83
+          stub(api).find('/first/path')  { stub(Dropbox::API::File.new).share_url { first_dropbox_url_payload } }
84
+          stub(api).find('/second/path') { stub(Dropbox::API::File.new).share_url { second_dropbox_url_payload } }
85
+          stub(api).find('/third/path')  { stub(Dropbox::API::File.new).share_url { third_dropbox_url_payload } }
86
+        end
87
+        @agent.options['link_type'] = 'permanent'
88
+      end
61 89
 
62
-      it 'creates one event with the temporary dropbox link for each path' do
63
-        expect { @agent.receive([@event]) }.to change(Event, :count).by(3)
64
-        last_events = Event.last(3)
65
-        expect(last_events[0].payload).to eq(first_dropbox_url_payload)
66
-        expect(last_events[1].payload).to eq(second_dropbox_url_payload)
67
-        expect(last_events[2].payload).to eq(third_dropbox_url_payload)
90
+      it 'creates one event with a single path' do
91
+        expect { @agent.receive([event(paths: '/first/path')]) }.to change(Event, :count).by(1)
92
+        expect(Event.last.payload).to eq(first_dropbox_url_payload.to_h)
68 93
       end
69 94
 
95
+      it 'creates one event with the permanent dropbox link for each path' do
96
+        event = event(paths: '/first/path, /second/path, /third/path')
97
+        expect { @agent.receive([event]) }.to change(Event, :count).by(3)
98
+        last_events = Event.last(3)
99
+        expect(last_events[0].payload).to eq(first_dropbox_url_payload.to_h)
100
+        expect(last_events[1].payload).to eq(second_dropbox_url_payload.to_h)
101
+        expect(last_events[2].payload).to eq(third_dropbox_url_payload.to_h)
102
+      end
70 103
     end
71
-
72 104
   end
73
-
74
-end
105
+end